{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import backtrader as bt \n",
    "import backtrader.indicators as btind \n",
    "import backtrader.feeds as btfeeds\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "type object 'datetime.datetime' has no attribute 'datetime'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_14764\\1125004969.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     33\u001b[0m     \u001b[0mdataname\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdatapath\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     34\u001b[0m         \u001b[1;31m# Do not pass values before this date\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 35\u001b[1;33m     \u001b[0mfromdate\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdatetime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdatetime\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m2023\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m7\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     36\u001b[0m         \u001b[1;31m# Do not pass values before this date\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     37\u001b[0m     \u001b[0mtodate\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdatetime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdatetime\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m2024\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m12\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m31\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mAttributeError\u001b[0m: type object 'datetime.datetime' has no attribute 'datetime'"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "import backtrader as bt\n",
    "\n",
    "# Create a subclass of Strategy to define the indicators and logic\n",
    "\n",
    "class SmaCross(bt.Strategy):\n",
    "    # list of parameters which are configurable for the strategy\n",
    "    params = dict(\n",
    "        pfast=10,  # period for the fast moving average\n",
    "        pslow=30   # period for the slow moving average\n",
    "    )\n",
    "\n",
    "    def __init__(self):\n",
    "        sma1 = bt.ind.SMA(period=self.p.pfast)  # fast moving average\n",
    "        sma2 = bt.ind.SMA(period=self.p.pslow)  # slow moving average\n",
    "        self.crossover = bt.ind.CrossOver(sma1, sma2)  # crossover signal\n",
    "\n",
    "    def next(self):\n",
    "        if not self.position:  # not in the market\n",
    "            if self.crossover > 0:  # if fast crosses slow to the upside\n",
    "                self.buy()  # enter long\n",
    "\n",
    "        elif self.crossover < 0:  # in the market & cross to the downside\n",
    "            self.close()  # close long position\n",
    "\n",
    "cerebro = bt.Cerebro()  # create a \"Cerebro\" engine instance\n",
    "\n",
    "# Create a data feed\n",
    "datapath='E:\\\\stock\\\\stock\\\\backtrader\\\\000001.SS.csv'\n",
    "\n",
    "    # Create a Data Feed\n",
    "data = bt.feeds.YahooFinanceCSVData(\n",
    "    dataname=datapath,\n",
    "        # Do not pass values before this date\n",
    "    fromdate=datetime.datetime(2023, 7, 1),\n",
    "        # Do not pass values before this date\n",
    "    todate=datetime.datetime(2024, 12, 31),\n",
    "        # Do not pass values after this date\n",
    "    reverse=False)\n",
    "\n",
    "cerebro.adddata(data)  # Add the data feed\n",
    "\n",
    "cerebro.addstrategy(SmaCross)  # Add the trading strategy\n",
    "cerebro.run()  # run it all\n",
    "cerebro.plot()  # and plot it with a single command"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
